/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author puspa
 */
public class Perpustakaan {

    public static String NAMA = "Perpustakaan Universitas Sanata Dharma Yogyakarta";
    public static ArrayList<Publikasi> DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
    public static ArrayList<Anggota> DAFTAR_ANGGOTA = new ArrayList<Anggota>();

    public Perpustakaan() {
    }

    public static void tambahPublikasi(Publikasi publikasi) {
        DAFTAR_PUBLIKASI.add(publikasi);
    }

    public static void tambahAnggota(Anggota anggota) {
        DAFTAR_ANGGOTA.add(anggota);
    }

    public static String bacaDaftarPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            return "kosong";
        } else {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                hasil += DAFTAR_PUBLIKASI.get(i).getJudul() + "\n";
            }
            return hasil;
        }
    }

    public static String bacaDaftarAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty()) {
            return "kosong";
        } else {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                hasil += DAFTAR_ANGGOTA.get(i).getNama() + "\n";
            }
            return hasil;
        }
    }

    public static void simpanFilePublikasi() {
        FileOutputStream outStream = null;
        if (!DAFTAR_PUBLIKASI.isEmpty()) {
            try {
                File outFile = new File("publikasi.dat");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
                outObjectStream.writeObject(DAFTAR_PUBLIKASI);
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } finally {
                try {
                    outStream.close();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Daftar publikasi masih kosong");
        }
    }

    public static void simpanFileAnggota() {
        FileOutputStream outStream = null;
        if (!DAFTAR_ANGGOTA.isEmpty()) {
            try {
                File outFile = new File("anggota.dat");
                outStream = new FileOutputStream(outFile);
                String data=new String();
                for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                    data += DAFTAR_ANGGOTA.get(i).getIdAnggota() + "\t"
                            + DAFTAR_ANGGOTA.get(i).getNama() + "\n";
                    
                }
                outStream.write(data.getBytes());
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } finally {
                try {
                    outStream.close();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Daftar Anggota masih kosong");
        }
    }

    public static void simpanFilePinjaman() {
    }

    public static void bacaFilePublikasi() {
        FileInputStream inStream = null;
        try {          
            File inFile = new File("publikasi.dat");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = new ObjectInputStream(inStream);
            DAFTAR_PUBLIKASI =(ArrayList<Publikasi>) inObjectStream.readObject();
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        finally {
            try {
                inStream.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }

    }

    public static void bacaFileAnggota() {
    }

    public static void bacaFilePinjaman() {
    }
}
